package game

import (
	"VirtualCamera/common"
	"VirtualCamera/models"
	"VirtualCamera/response"
	"fmt"

	"VirtualCamera/controllers"
	//"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type GameController struct {
	controllers.BaseController
}
// 获取所有的游戏列表
func (con GameController) GetGameList(c *gin.Context) {

	db := common.GetDB()

	// 获取全部记录
	var games []models.Game
	// SELECT * FROM users;
	result := db.Find(&games)
	fmt.Println(result.RowsAffected) // 返回找到的记录数，相当于 `len(users)`
	if result.Error != nil{
		response.Response(c, http.StatusOK, 400, nil, result.Error.Error())
	}
	ret := gin.H{
		"gameList": games,
		"length": result.RowsAffected,
	}
	// returns error
	//var requestUser = models.Game{}

	//c.ShouldBind(&requestUser)
	//fmt.Println(requestUser)
	//
	//username := requestUser.Name
	//
	////判断用户名是否存在,不存在才可以注册
	//var user models.User
	//db.Where("name = ? ", username).Find(&user)
	//if user.ID != 0 {
	//	response.Response(c, http.StatusOK, 400, nil, "用户名已存在")
	//	return
	//}
	//
	////用户名不存在 可以增加进入数据库
	//user.Name = requestUser.Name
	//user.Password = requestUser.Password
	//user.Email = requestUser.Email
	//user.Telephone = requestUser.Telephone
	//
	//result := db.Create(&user)
	//if result.Error != nil {
	//	fmt.Println(result.Error)
	//	response.Response(c, http.StatusOK, 400, nil, result.Error.Error())
	//	return
	//}

	response.Success(c, ret, "返回成功")

}
// 获取指定年龄段的游戏列表
func (con GameController) GetGamesByAge(c *gin.Context) {
	db := common.GetDB()
	age := c.Param("age")
	fmt.Println("age",age)
	var games []models.Game
	result := db.Where("age = ?", age).Find(&games)
	//username := c.Query("Name")
	length := result.RowsAffected
	err := result.Error
	if (err != nil){
		response.Response(c, http.StatusOK, 400, nil, "游戏不存在")
	}
	fmt.Println(length)
	//response.Response(c, http.StatusOK, 400, nil, "用户不存在")
	// var requestUser = models.UserInfo{}
	// c.Bind(&requestUser)

	// username := requestUser.Name
	// fmt.Println(requestUser)
	//fmt.Println(username)
	//判断用户名是否存在,不存在则失败
	//var user models.User
	//db.Where("name = ? ", username).Find(&user)
	//if user.ID == 0 {
	//	response.Response(c, http.StatusOK, 400, nil, "用户不存在")
	//	return
	//}

	ret := gin.H{
		"game":  games,
		"length": length,
	}
	response.Success(c, ret, "查询成功")
}
